import { Controller } from "../../mvc/core/Controller";
import { IConsumeDayGiftBagController } from "./interfaces/IConsumeDayGiftBagController";
import { ConsumeCountIcon } from "../view/consumeCount/ConsumeCountIcon";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { ConsumeCountGiftBagModule } from "../../../modules/ConsumeCountGiftBagModule";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { ConsumeDayData } from "../view/consumeCount/ConsumeDayData";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { GameProxy } from "../mvc/GameProxy";
import { TOpenRechargeGiftBag } from "../../../Message/Db/Tables/TOpenRechargeGiftBag";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ClockManager } from "../manager/ClockManager";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { IConsumeCountGiftBagModule } from "../../../modules/interfaces/IConsumeCountGiftBagModule";
type int = number;
//class ConsumeDayGiftBagController
    
    export  class ConsumeDayGiftBagController extends Controller implements IConsumeDayGiftBagController
    {
       

        public get consumeCountIcon(): ConsumeCountIcon
        {
            if (this._consumeCountIcon == null) 
            {
                this._consumeCountIcon = new ConsumeCountIcon();
                this._consumeCountIcon.updateIconInfo();
                this._consumeCountIcon.addEventListener(EventName.ConsumeCountIconClick, this.onConsumeCountIconClickHandler);
            }
            return this._consumeCountIcon;
        }

        protected /* override */ initView(): IView
        {
            if (this._consumeDayGiftBag == null) 
            {
                this._consumeDayGiftBag = new ConsumeCountGiftBagModule();
                this._consumeDayGiftBag.type = GModuleEvent.Type_ConsumeDay;
            }
            return this._consumeDayGiftBag;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ConsumeDayGetCodeSuccess, this.onGetCodeSuccessHandler);
            Dispatcher.addEventListener(EventName.ConsumeDayHadGetCodeSuccess, this.onHadGetCodeSuccessHandler);
            Dispatcher.addEventListener(EventName.ConsumeDayGetGiftBag, this.onConsumeDayGetGiftBagHandler);
            Dispatcher.addEventListener(EventName.ConsumeDayItemSelected, this.onConsumeDayItemSelectedHandler);
            Dispatcher.addEventListener(EventName.ConsumeIsGetGift, this.onGiftIsCanGet);
            NetDispatcher.addCmdListener(ServerCommand.ConsumeTodayActiveCommand, this.onConsumeTodayActiveCommandHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateActiveConsumeDay, this.onActiveConsumeDayHandler);
            return;
        }

        /* internal  */onGiftIsCanGet(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (loc1 == 5 || loc1 == 6) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            return;
        }

        /* internal  */isGetAllGift(): void
        {
            var loc1=ConsumeDayData.isHadGetAllGift(EActiveType._EActiveTypeConsumeDay1);
            var loc2=ConsumeDayData.isHadGetAllGift(EActiveType._EActiveTypeConsumeDay2);
            if (loc1 && loc2) 
            {
                this.removeConsumeCountActive();
            }
            return;
        }

        /* internal  */onGetCodeSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            this.updateHadGetCodes(loc1);
            return;
        }

        /* internal  */updateHadGetCodes(arg1: Array<any>): void
        {
            ConsumeDayData.hadGetDayCodes = arg1;
            if (this._consumeDayGiftBag) 
            {
                this._consumeDayGiftBag.updateGiftBag();
                this._consumeDayGiftBag.updateConsumeCountGold(cache.role.consumeDay);
            }
            this.isGetAllGift();
            if (ConsumeDayData.isCanGetGift()) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            else 
            {
                this.consumeCountIcon.setFlashEffect(false);
            }
            return;
        }

        /* internal  */onHadGetCodeSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.hadGetCodes as Array<any>;
            var loc3=loc1.date as Date;
            this.updateHadGetCodes(loc2);
            ConsumeDayData.allActiveEndTime = loc3;
            return;
        }

        /* internal  */onConsumeDayGetGiftBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getConsumeDayGiftBag(loc1);
            this._consumeCountIcon.setFlashEffect(false);
            return;
        }

        /* internal  */onConsumeCountIconClickHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
                this._consumeDayGiftBag.type = GModuleEvent.Type_ConsumeDay;
                GameProxy.roleProxy.hadGetConsumeDayGiftBag();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onConsumeDayItemSelectedHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as TOpenRechargeGiftBag;
            if (this._consumeDayGiftBag) 
            {
                this._consumeDayGiftBag.updateItemList(loc1);
            }
            return;
        }

        /* internal  */onConsumeTodayActiveCommandHandler(arg1: SActiveToClient): void
        {
            var loc1=cache.active.isActiveInEffect(arg1.code);
            if (loc1) 
            {
                ConsumeDayData.activeToClient = arg1;
                ClockManager.instance.addDateCall(arg1.endDt, this.removeConsumeCountActive);
                if (ConsumeDayData.isRechargeActive && !ConsumeDayData.isHadGetAllGift(arg1.code)) 
                {
                    this.consumeCountIcon.show();
                }
                if (this._consumeDayGiftBag) 
                {
                    this._consumeDayGiftBag.type = GModuleEvent.Type_ConsumeDay;
                }
            }
            if (loc1 && !this._isGetCodes && !ConsumeDayData.isHadGetAllGift(arg1.code)) 
            {
                GameProxy.roleProxy.hadGetConsumeDayGiftBag();
                this._isGetCodes = true;
            }
            return;
        }

        /* internal  */removeConsumeCountActive(): void
        {
            if (this._consumeCountIcon && !this._consumeCountIcon.isHide) 
            {
                this.consumeCountIcon.hide();
            }
            if (this._consumeDayGiftBag && !this._consumeDayGiftBag.isHide) 
            {
                view.hide();
            }
            if (ConsumeDayData.activeToClient) 
            {
                ClockManager.instance.removeDateCall(ConsumeDayData.activeToClient.endDt);
            }
            this._isGetCodes = false;
            return;
        }

        /* internal  */onActiveConsumeDayHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            cache.role.consumeDay = loc1.value;
            cache.role.consumeWorldLevle = parseInt(loc1.valueStr);
            if (this._consumeDayGiftBag) 
            {
                this._consumeDayGiftBag.updateGiftArr();
                this._consumeDayGiftBag.updateGiftBag();
                this._consumeDayGiftBag.updateConsumeCountGold(loc1.value);
            }
            if (ConsumeDayData.isCanGetGift()) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            return;
        }

        private /* var */_consumeDayGiftBag: IConsumeCountGiftBagModule;

        private /* var */_consumeCountIcon: ConsumeCountIcon;

        private /* var */_isGetCodes: Boolean=false;
    }
